/*
 * @lc app=leetcode.cn id=1275 lang=typescript
 *
 * [1275] 找出井字棋的获胜者
 */

// @lc code=start

// 模拟法。。。。
function tictactoe(moves: number[][]): string {
    const panel = Array(3)
        .fill("")
        .map((i) => {
            return Array(3).fill("");
        });
    // 构建交叉的数据
    const temp = [...Array(3).keys()];
    const cross = [
        temp.map((i) => [i, i]),
        // 注意是 index 故为 2
        temp.map((i) => [i, 2 - i]),
    ];
    const check = (): string | false => {
        const rowSuss = panel.find((row) => {
            return row.every((i) => {
                return i !== "" && i == row[0];
            });
        });
        if (rowSuss) return rowSuss[0];

        // 列检查
        for (let i = 0; i < 3; i++) {
            let Suss: string | false = panel[0][i];
            for (let j = 0; j < 3; j++) {
                if (panel[j][i] === "" || panel[j][i] !== panel[0][i])
                    Suss = false;
            }
            if (Suss) return Suss;
        }

        for (const iterator of cross) {
            const [[x, y], ...others] = iterator;
            const first = panel[y][x];
            if (first === "") continue;
            const result = others.every(([x, y]) => {
                return first === panel[y][x];
            });
            if (result) {
                return first;
            }
        }

        return false;
    };
    const setPanel = ([x, y]: number[], who: string) => {
        panel[y][x] = who;
    };
    // 执行
    for (let i = 0; i < moves.length; i++) {
        const element = moves[i];
        setPanel(element, i % 2 ? "X" : "O");
        const result = check();
        if (result) {
            // 转换 OX 为 AB
            return result === "O" ? "A" : "B";
        }
    }

    if (moves.length === 9) return "Draw";
    return "Pending";
}
// @lc code=end
